const MongoClient = require('mongodb').MongoClient;
// const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  const sub_tab = '大数据';
  let topics = await db.collection('topics')
    .find(
      {author_name: '51CTO', sub_tab: sub_tab}
    )
    .toArray();


  for (let i = 0; i < topics.length; i++) {
    let topic = topics[i];
    if (topic.boards) {
      topic.boards.push(sub_tab);
    } else {
      topic.boards = [sub_tab];
    }
    topic.boards = [...new Set(topic.boards)];
    let result = await db.collection('topics').updateOne({_id: topic._id}, {
      $set: {
        boards: topic.boards
      },
    });
    console.log(topic.boards, i, topics.length);
  }

  client.close();

}

main();
